//https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/

class Solution {
public:
    TreeNode* build(vector<int>& inorder, vector<int>& postorder, int& p, int inbegin, int inend)
    {
        if (inbegin > inend)
            return NULL;

        //确定根节点
        TreeNode* root = new TreeNode(postorder[p]);

        //确定区间
        int rooti = inbegin;
        while (rooti <= inend)
        {
            if (postorder[p] == inorder[rooti]) break;
            else
                rooti++;
        }

        //递归创建
        p--;
        root->right = build(inorder, postorder, p, rooti + 1, inend);
        root->left = build(inorder, postorder, p, inbegin, rooti - 1);

        return root;
    }
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        int i = postorder.size() - 1;
        TreeNode* root = build(inorder, postorder, i, 0, inorder.size() - 1);

        return root;

    }
};